package com.global.college.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具类
 */
public class TimeUtils {
    public static String getNowTime() {//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String getNowDate() {//获取当前日期
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    //判断选择的日期是否是今天
    public static boolean isToday(long time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    private static boolean isThisTime(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * 毫秒转化成时：分：秒
     * @param time
     * @return
     */
    /**
     * 获取小时/分钟/秒
     *
     * @param
     * @return 包含小时、分钟、秒的时间字符串，例如3小时23分钟13秒。
     */
    public static String getTimeMS(String s) {//忽略小时
        int second = Integer.valueOf(s) / 1000;
        int hour = second / (60 * 60);
        int minute = (second - (hour * 60 * 60)) / 60;
        long sec = (second - (hour * 60 * 60) - minute * 60);
        String strHour = String.valueOf(hour);
        String strMin = String.valueOf(minute);
        String strSec = String.valueOf(sec);
        if (strHour.length() == 1) {
            strHour = "0" + strHour;
        }
        if (strMin.length() == 1) {
            strMin = "0" + strMin;
        }
        if (strSec.length() == 1) {
            strSec = "0" + strSec;
        }
        return strHour + ":" + strMin + ":" + strSec;

    }

    /**
     * 把时间戳转换成yyyy-MM-dd格式
     */

    public static String stampToDate(String s) {
        if (s.equals("")) {
            return "----";
        }
        String res;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 把时间戳转换成yyyy-MM-dd  HH:mm:ss格式
     */
    public static String stampToDateTime(String s) {
        if (s.equals("")) {
            return "暂无";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 把时间戳转换成yyyy-MM-dd  HH:mm:ss格式
     */
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDateTime2(String s) {
        if (s.equals("")) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 把时间戳转换成yyyy-MM-dd  HH:mm:ss格式
     */
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDateTime3(String s) {
        if (s.equals("")) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        res = simpleDateFormat.format(date);
        return res;
    }


    //获取小时
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(calendar.HOUR_OF_DAY);
    }

    //获取分钟
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(calendar.MINUTE);
    }

    //获取周
    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    //获取周
    public static int getWeek(int year, int moth, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, moth - 1, day);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    //获取年
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(calendar.YEAR);
    }

    //获取月
    public static int getMoth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(calendar.MONTH) + 1;
    }

    //获取日
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(calendar.DATE);
    }

    public static Date getDate(int year, int moth, int day, int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, moth - 1, day, hour, minute);
        return calendar.getTime();
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }


    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        try {
            Date date = format.parse("2016-12-15 12");
            System.out.println(getHour(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /*
     * 将时间转换为时间戳
     */
    public static Long dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        return ts;
    }

    /**
     *    * 获取自定义时间的年/月/日
     *    
     */
    public static void getMonthDay2Set(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date dt = null;
        try {
            dt = sdf.parse(dateStr);
            cal.setTime(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
    }

    /**
     * 获取时间/时间戳的年
     * @param dateStr
     * @return
     */
    public static int getYears(String dateStr) {
        if (StringUtils.isEmpty(dateStr))
            return 2000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date dt = null;
        try {
            dt = sdf.parse(dateStr);
            cal.setTime(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.YEAR);

    }
    /**
     * 获取时间/时间戳的月
     * @param dateStr
     * @return
     */
    public static int getMonths(String dateStr) {
        if (StringUtils.isEmpty(dateStr))
            return 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date dt = null;
        try {
            dt = sdf.parse(dateStr);
            cal.setTime(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.MONTH) + 1;

    }
    /**
     * 获取时间/时间戳的日
     * @param dateStr
     * @return
     */
    public static int getDays(String dateStr) {
        if (StringUtils.isEmpty(dateStr))
            return 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date dt = null;
        try {
            dt = sdf.parse(dateStr);
            cal.setTime(dt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cal.get(Calendar.DAY_OF_MONTH);

    }

    /**
     * 计算某时间戳和当前时间的差
     */
    public static long getLeftTime(long timestamp){
        if (timestamp == 0)
            return 0;
        long  newstamp = System.currentTimeMillis();
        long leftime = (newstamp - (timestamp*1000))/1000;
        if (leftime > 0){
            return leftime;
        }
        return 0;

    }
    public static String getTimeStamp(){
        String res = String.valueOf(System.currentTimeMillis());
        return res;
    }
}
